/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 * Description: mc2 profiling
 * Author: huawei
 * Create: 2024-05-24
 */

#ifndef ASCEND_ACE_COMOP_HCCL_HCCL_AI_CPU_KERNEL_DFX_PROFILING_PROFILING_MANAGER_H_
#define ASCEND_ACE_COMOP_HCCL_HCCL_AI_CPU_KERNEL_DFX_PROFILING_PROFILING_MANAGER_H_
#include "external/hccl/hccl_types.h"
#include "stream_pub.h"
#include "hccl_common.h"
#include "common/aicpu_hccl_def.h"

extern "C" {
__attribute__((weak)) int32_t AdprofReportAdditionalInfo(uint32_t agingFlag, const void *data, uint32_t length);
__attribute__((weak)) int32_t AdprofCheckFeatureIsOn(uint64_t feature);
__attribute__((weak)) uint64_t AdprofGetHashId(const char *hashInfo, size_t length);
};
namespace dfx {

struct ProfCommInfo {
    uint64_t groupNameHashId{0};
    u32 rankNum{0};
    u32 rankId{0};

    ProfCommInfo() {};

    ProfCommInfo(uint64_t groupNameHashId, u32 rankNum, u32 rankId)
        : groupNameHashId(groupNameHashId), rankNum(rankNum), rankId(rankId)
    {};
};

class ProfilingManager {
public:
    /**
     * 上报kfc的多轮执行时刻的分段信息，只涉及主流
     */
    static HcclResult ReportTaskExecTimeLine(AicpuComProf *acprof);
    /**
     * 上报kfc展开的多轮的task的信息
     */
    static HcclResult ReportTaskInfo();
    static HcclResult CallMsprofReportAdditionInfo(uint32_t type, uint64_t timeStamp, const void *data, int len);
    static bool IsProfOn(uint64_t feature);

    /* AICPU profiling*/
    static bool IsProfL1On();
    static bool IsProfL0On();
    static bool IsL1fromOffToOn();
    static HcclResult ReportTaskInfo(hccl::Stream stream);
    static HcclResult ReportHcclOpInfo(MsprofAicpuHCCLOPInfo& hcclOpInfo, std::string &algTypeStr);
    static uint64_t GetProfHashId(const char *name, uint32_t len);
    static HcclResult GetProfInfoByStreamId(s32 streamId, ProfCommInfo& profInfo);
    static HcclResult AddProfInfoByStreamId(s32 streamId, const std::string &tag, const ProfCommInfo& profInfo);
    static HcclResult UpdateStartReportSqeIdx(s32 streamId, u32 newSqeTailIdx);
    static uint32_t GetStartReportSqeIdx(s32 streamId);
    static bool GetProfL0State();
    static bool GetProfL1State();
    static HcclResult ReportMainStreamTask(hccl::Stream& stream, uint16_t taskId, uint16_t type);
    static HcclResult ReportFilpTask(s32 streamId,  uint16_t taskId, uint32_t flipNum);
    static uint64_t TransferAlgType(AlgType algType);
    static void DumpHcclInfo(const MsprofAicpuHcclTaskInfo& taskInfo, u32 batchId, u32 idx);
    static void CommInfo2HcclInfo(const dfx::ProfCommInfo &profInfo, MsprofAicpuHcclTaskInfo &taskInfo);

private:
    static std::mutex streamMutex_;
    static std::unordered_map<std::string, ProfCommInfo> tagOpInfoMap_;
    static std::unordered_map<s32, std::string> streamToTagMap_;

    static std::mutex startReportSqeIdxMutex_;
    static std::unordered_map<s32, u32> streamToSqeIdxMap_;
    static bool isL0Open_;
    static bool isL1Open_;
};

}  // namespace dfx
#endif  // ASCEND_ACE_COMOP_HCCL_HCCL_AI_CPU_KERNEL_DFX_PROFILING_PROFILING_MANAGER_H_
